﻿using System;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls.GridView;
using System.Collections.Generic;
using Baosight.ZNH.ZP.UI.PopWindows;
using Baosight.ZNH.ZP.UI.Utils;

namespace Baosight.ZNH.ZP.UI
{
    public partial class ZP09 : EFViewCmes
    {
        private BaseBlock block = new BaseBlock();//构造前台下拉列表
        private string actionType;//操作类型
        public ZP09()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }


        /// <summary>
        /// 修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.Grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 页面初始化操作
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(EIInfo initData)
        {
            initData.AddBlock(block.GetYesOrNo());
            base.Initialize(initData);
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "人员-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.Grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.Grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            base.OnUpdateButtonClickPopup(eiInfo, "人员信息-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 删除按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.Grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.Grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            base.OnDeleteButtonClick(sender, e);
        }

        /// <summary>
        /// 启用按钮的执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnStartButtonClick(object sender, RoutedEventArgs e)
        {
            this.actionType = "start";
            base.OnModifyButtonClick("start", "启用", sender, e);
        }
 
            /// <summary>
        /// 停用按钮的执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnStopButtonClick(object sender, RoutedEventArgs e)
        {
            this.actionType = "stop";
            base.OnModifyButtonClick("stop", "停用", sender, e);
        }

        /// 密码重置按钮的执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnResetButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> row = this.Grid.CheckedEIRows;
            if (row.Count == 0)
            {
                base.OpenEFWindowAlert("请先选择要重置密码的用户信息记录",true);
                return;
            }
            else
            {
                DialogParameters param = this.ModelDialogParameters("Confirm", "是否要对选中的记录重置密码？");
                param.Closed = ResetConfirmClosed;
                EFWindow.Confirm(param);
            }
        }
        private void ResetConfirmClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null || !e.DialogResult.Value)
            {
                this.TempButtonOpen(true);//取消后，“改变状态”按钮生效
                return;
            }

            if (e.DialogResult.Value)
            {
                this.Submit(OperationMode.Update, "ZP09", "reset", DataRange.CheckedItems, ResetCompleted);
            }
        }


        private void ResetCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Attributes.ContainsKey("formStatus") && e.EIInfo.Attributes["formStatus"].ToString().Equals("1"))
            {
                this.Grid.Refresh();
                base.OpenEFWindowAlert("用户密码重置成功！",true);
                return;
            }
            if (e.EIInfo.GetStatus() == -1)
            {
                base.OpenEFWindowAlert("用户密码重置失败", true);
                return;
            }
        }

        /// <summary>
        /// 迁移按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnMoveButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            List<EIRow> EFGridCheckedRows = this.Grid.CheckedEIRows;
            int count = EFGridCheckedRows.Count;
            if (count <= 0)
            {
                BaseTools.OpenEFWindowAlert("请选择记录进行迁移！", WindowAlertClosed);
                return;
            }
            else
            {
                PersonMove personMove = new PersonMove(this.orgCode.Value.ToString(), EFGridCheckedRows);
                CmesWindows _win = new CmesWindows();//构造一个弹出窗口
                _win.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
                _win.Header = personMove.eTitle;//设置Title
                _win.Content = personMove;
                _win.Closed += CmesWindow_Closed;
                _win.ShowDialog();//模态显示弹出窗口
            }
        }


        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.Grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            AddPerson addPerson = new AddPerson(_dialogType);//构造一个新的修改页面
            addPerson.EIDataContext = new Platform.Core.UI.EIDataContext();
            if ("insert".Equals(_dialogType))
            {
                addPerson.orgCode.Value = this.orgCode.Value;
                addPerson.dept.Value = this.orgName.Value;
                
            }
            addPerson.EIDataContext.Update(_eIInfo);
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = addPerson;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "人员信息-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 停用、启用前的判断
        /// </summary>
        /// <param name="rowList"></param>
        /// <returns></returns>
        public override bool BeforeMethod(List<EIRow> _eIRows)
        {
            bool Flag = true;
            if (this.actionType != null && this.actionType == "stop")
            {
                foreach (var row in _eIRows)
                {
                    if ("0".Equals(row["valid"].ToString()))
                    {
                        base.OpenEFWindowAlert("所选记录中包含停用状态的记录！", true);
                        Flag = false;
                        break;
                    }
                }
            }
            else if (this.actionType == "start")
            {
                foreach (var row in _eIRows)
                {
                    if ("1".Equals(row["valid"].ToString()))
                    {
                        base.OpenEFWindowAlert("所选记录中包含启用状态的记录！", true);
                        Flag = false;
                        break;
                    }
                }
            }
            this.actionType = null;
            return Flag;
        }
    }
}
